package com.dyna.cp.sms;

import com.alibaba.fastjson.JSON;
import com.dyna.domain.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.comm.enumerate.SupplierType;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api("demo模块/cp/短信功能")
@RestController
@RequestMapping("/sms")
public class SmsController {

    private final SmsService smsService;

    @Autowired
    public SmsController(SmsService smsService) {
        this.smsService = smsService;
    }

    @ApiOperation("短信发送")
    @RequestMapping("/sendSms")
    public R<String> sendSms(@RequestParam("phone") String phone, @RequestParam("content") String content) {
        return R.success(smsService.sendSms(phone,content));
    }

}

@Slf4j
@Service
class SmsService {

    public String sendSms(String phone, String content) {
        SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.ALIBABA);
        SmsResponse smsResponse = smsBlend.sendMessage(phone, content);
        log.info("短信发送结果:{}", JSON.toJSONString(smsResponse));
        return smsResponse.getMessage();
    }

}